#!/usr/bin/env python
import os
from app import create_app, db
from flask_migrate import Migrate, MigrateCommand
from flask_script import Manager, Shell, Server
from app.models import user

app = create_app(os.getenv('FLASK_CONFIG') or 'default')
manager = Manager(app)
migrate = Migrate(app, db)


def make_shell_context():
	return dict(app=app, db=db)


manager.add_command("shell", Shell(make_context=make_shell_context))
manager.add_command('db', MigrateCommand)


@manager.command
def drop():
	db.drop_all()


@manager.command
def create():
	db.create_all()


@manager.command
def deploy(deploy_type="development"):
	from flask_migrate import upgrade
	from app_config import UserInit
	upgrade()
	if deploy_type == 'production':
		user.User.insert_admin_user(UserInit["u1"])
		user.User.insert_admin_user(UserInit["u2"])
		user.User.insert_admin_user(UserInit["u3"])

	if deploy_type == 'development':
		user.User.insert_admin_user(UserInit["u1"])
		user.User.insert_admin_user(UserInit["u2"])
		user.User.insert_admin_user(UserInit["u3"])


if __name__ == '__main__':
	"""
	before runserver, export FLASK_ENV=development, to remove WARNING: 
	Do not use the development server in a production environment.
	"""
	manager.add_command("runserver", Server(host="0.0.0.0", port=5000, use_debugger=True, use_reloader=True))
	manager.run()
